package org.example.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
public class IndexController {

    @GetMapping("/sse")
    public SseEmitter sse() throws IOException {
        SseEmitter sseEmitter = new SseEmitter();
        new Thread(() -> {
            while(true) {
                try {
                    sseEmitter.send("1232");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
        return sseEmitter;
    }
}
